﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;

namespace BTGISWASECO.Data
{
    public static class DatasetHelper
    {
        public static IFeatureLayer OpenFeatureLayer(string directory, string name)
        {
            IWorkspaceFactory factory = new ShapefileWorkspaceFactory();
            IWorkspace ws = factory.OpenFromFile(directory, 0);

            IFeatureWorkspace fws = (IFeatureWorkspace)ws;
            
            IFeatureLayer fl = new FeatureLayerClass();
            fl.FeatureClass = fws.OpenFeatureClass(name);
            return fl;
        }

        public static ITable OpenTableFromDB(string databaseName, string tableName)
        {
            ITable t = null;
            IWorkspaceFactory factory = new AccessWorkspaceFactoryClass();
            IFeatureWorkspace fws = (IFeatureWorkspace)factory.OpenFromFile(databaseName, 0);
            t = fws.OpenTable(tableName);
            return t;
        }

        public static ITable OpenTable(string directory, string tableName)
        {
            ITable t = null;
            IWorkspaceFactory factory = new ShapefileWorkspaceFactory();
            IFeatureWorkspace fws = (IFeatureWorkspace)factory.OpenFromFile(directory, 0);
            t = fws.OpenTable(tableName);
            return t;
        }


        public static void ClearTable(ITable t)
        {
            // create an update cursor
            ICursor crs = t.Update(null, false);
            
            IRow r = crs.NextRow();
            while (r != null)
            {
               crs.DeleteRow();
               r = crs.NextRow();
            }
        }
    }
}
